/**
 * @(#)Activator.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import edu.pku.sei.pgie.analyzer.core.ICodeEntityRelation;
import edu.pku.sei.pgie.analyzer.internal.relation.AbstractCodeEntityRelation;

/**
 * The main plugin class to be used in the desktop.
 *
 * @author generated
 */
public class Activator extends AbstractUIPlugin {
	private static Log logger = LogFactory.getLog(Activator.class);

	// The shared instance.
	private static Activator plugin;

	public static final String PLUGINID = "edu.pku.sei.pgie";

	private List<ICodeEntityRelation> codeEntityRelations;

	/**
     * The constructor.
     */
	public Activator() {
		plugin = this;
		codeEntityRelations = new ArrayList<ICodeEntityRelation>();
	}

	/**
     * This method is called upon plug-in activation
     */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		IExtensionRegistry reg = Platform.getExtensionRegistry();
		IConfigurationElement[] extensions = reg
				.getConfigurationElementsFor("edu.pku.sei.pgie.CodeEntityRelationContribution");
		if (extensions != null && extensions.length > 0) {
			for (int i = 0; i < extensions.length; i++) {
				IConfigurationElement configuration = extensions[i];
				String className = configuration.getAttribute("class");
				String description = configuration.getAttribute("description");
				String enable = configuration.getAttribute("enable");
				String id = configuration.getAttribute("id");
				logger.info("load configuration point[id="+id+",class="+className+",description="+description+",enable="+enable+"]");
				if(enable != null && Boolean.parseBoolean(enable)) {
					if(className != null) {
						ICodeEntityRelation relation = null;
						try {
							relation = (ICodeEntityRelation) Class.forName(className).newInstance();
						} catch(Exception e) {
							logger.debug("load code entity relation [class="+className+"] failed");
						}
						if(relation != null) {
							((AbstractCodeEntityRelation)relation).setRelationName(description);
							codeEntityRelations.add(relation);
						}
					}
				}
			}
		}
	}

	/**
     * This method is called when the plug-in is stopped
     */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
		codeEntityRelations.clear();
	}

	/**
     * Returns the shared instance.
     */
	public static Activator getDefault() {
		return plugin;
	}

	/**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path.
     *
     * @param path
     *            the path
     * @return the image descriptor
     */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin(
				PLUGINID, path);
	}

	public List<ICodeEntityRelation> getCodeEntityRelations() {
		return codeEntityRelations;
	}
}
